package com.linkgie.galaxyframework.httpproxy.core;

import com.linkgie.galaxyframework.httpproxy.HttpRequestDefinition;
import com.linkgie.galaxyframework.httpproxy.ResponseConverter;
import com.linkgie.galaxyframework.httpproxy.ResponseConverterFactory;
import com.linkgie.galaxyframework.httpproxy.ResponseHandler;
import com.linkgie.galaxyframework.httpproxy.ResponseHandlerFactory;
import com.linkgie.galaxyframework.httpproxy.converters.DefaultResponseHandler;
import com.linkgie.galaxyframework.httpproxy.converters.DefaultResponseHandlerFactory;
import com.linkgie.galaxyframework.httpproxy.converters.ResponseConverterHandlerAdapter;

/**
 * 适配旧的 {@link ResponseConverterFactory} 为 {@link ResponseHandlerFactory}；
 */
@SuppressWarnings("deprecation")
public class ResponseConverterHandlerFactoryAdapter implements ResponseHandlerFactory {

    private ResponseConverterFactory responseConverterFactory;

    public ResponseConverterHandlerFactoryAdapter(ResponseConverterFactory responseConverterFactory) {
        this.responseConverterFactory = responseConverterFactory;
    }

    @Override
    public ResponseHandler createResponseHandler(HttpRequestDefinition request, Class<?> returnType) {
        ResponseConverter converter = responseConverterFactory.createResponseConverter(request, returnType);

        return createResponseHandler(converter);
    }

    public static ResponseHandler createResponseHandler(ResponseConverter converter) {
        ResponseHandler successHandler = new ResponseConverterHandlerAdapter(converter);
        return new DefaultResponseHandler(successHandler, DefaultResponseHandlerFactory.DEFAULT_ERROR_HANDLER);
    }

}
